package com.gnerv.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.cms.common.Result;
import com.gnerv.cms.dto.ArticleDTO;
import com.gnerv.cms.dto.ArticleDetailsDTO;
import com.gnerv.cms.dto.ArticleSaveDTO;
import com.gnerv.cms.dto.ArticleSearchDTO;
import com.gnerv.cms.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * CMS-文章表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-10-10
 */
@Api(tags = "文章管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/cms/article")
public class ActicleController {

    @Resource
    private IArticleService articleService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveArticle(@RequestBody ArticleSaveDTO articleSaveDTO) {
        articleService.saveArticle(articleSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createArticle(@RequestBody ArticleSaveDTO articleSaveDTO) {
        articleService.createArticle(articleSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "/delete/{articleId}", method = RequestMethod.GET)
    public Result deleteByArticleId(@PathVariable(value = "articleId") String articleId) {
        articleService.deleteByArticleId(articleId);
        return Result.response();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateArticle(@RequestBody ArticleSaveDTO articleSaveDTO) {
        articleService.updateArticle(articleSaveDTO);
        return Result.response();
    }

    @RequestMapping(value = "/{articleId}/status/{status}", method = RequestMethod.GET)
    public Result updateArticleStatus(@PathVariable(value = "articleId") String articleId,
                                      @PathVariable(value = "status") Integer status) {
        articleService.updateArticleStatus(articleId, status);
        return Result.response();
    }

    @RequestMapping(value = "/select/{articleId}", method = RequestMethod.GET)
    public Result selectArticle(@PathVariable(value = "articleId") String articleId) {
        ArticleDTO article = articleService.selectArticle(articleId);
        return Result.response().data("article", article);
    }

    @RequestMapping(value = "/select/details/{articleId}", method = RequestMethod.GET)
    public Result selectArticleDetails(@PathVariable(value = "articleId") String articleId) {
        ArticleDetailsDTO articleDetails = articleService.selectArticleDetails(articleId, null);
        return Result.response().data("articleDetails", articleDetails);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listArticle(ArticleSearchDTO article,
                              @RequestParam(value = "current", defaultValue = "1") Long current,
                              @RequestParam(value = "size", defaultValue = "300") Long size) {
        Page<ArticleDTO> articles = articleService.listArticle(article, current, size);
        return Result.response().data("articles", articles);
    }

}
